package team.niit.dao.impl;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import team.niit.dao.AdminDAO;
import team.niit.entity.AdminEntity;
import team.niit.entity.StudentEntity;
import team.niit.util.SessionFactoryUtil;

/**
 * @Author: HAPPY
 * @Project_name: JSF
 * @Package_name: team.niit.dao.impl
 * @Date: 2021/10/26 20:12
 * Explain:
 */
public class AdminDAOImpl implements AdminDAO {

    /**
     * 管理员登录验证
     *
     * @param id       工号
     * @param password 密码
     * @return
     */
    @Override
    public Object login(String id, String password) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        String hql = "from team.niit.entity.AdminEntity admin where admin.id = :id and admin.password= :password";
        Query query = session.createQuery(hql);
        query.setParameter("id", id);
        query.setParameter("password", password);
        if (query.list().size() > 0) {
            return query.list().get(0);
        } else {
            return null;
        }
    }

    /**
     * 重置密码/找回密码
     *
     * @param id       工号
     * @param password 新密码
     * @param phone    手机号
     * @return 返回成功与否
     */
    @Override
    public boolean retrievePassword(String id, String password, String phone) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String hql = "update from team.niit.entity.AdminEntity admin set admin.password=:password where admin.id = :id and admin.phone= :phone";
        Query query = session.createQuery(hql);
        query.setParameter("password", password);
        query.setParameter("id", id);
        query.setParameter("phone", phone);
        int num = query.executeUpdate();
        transaction.commit();
        if (num > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean modifyPassword(String id, String oldPassword, String newPassword) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "update from team.niit.entity.AdminEntity admin set admin.password=:newPassword where admin.id = :id and admin.password= :oldPassword";
        Query query = session.createQuery(hql);
        query.setParameter("newPassword", newPassword);
        query.setParameter("id", id);
        query.setParameter("oldPassword", oldPassword);
        int num = query.executeUpdate();
        tx.commit();
        if (num > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public AdminEntity modifyAdminById(AdminEntity admin) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "update from team.niit.entity.AdminEntity admin set admin.phone=:phone where admin.id = :id";
        Query query = session.createQuery(hql);
        query.setParameter("phone", admin.getPhone());
        query.setParameter("id", admin.getId());
        query.executeUpdate();
        tx.commit();
        return admin;
    }
}
